Ccay.Module.define("Ccay.registry.ListRegistry", function(page, $S) {
	//避免多次查找form
	var form=$S("#registryForm");
	//避免多次获取treeManager
	var tree;

	/**
	 * 页面初始化
	 */
	page.ready=function(){
		page.initRegistryTree();
	};
	/**
	 * 初始化tree
	 */
	page.initRegistryTree = function(defaultSelect){
		//zTree设置
		var treeSetting={
				filter:true,
				onClick:page.onTreeClick
		}; 
    	 $S('#registryTree').ccayTree("services/manage/registry/list",treeSetting,function(data){
	     	tree=$S('#registryTree').getCcayTreeManager();	 
	     	if(!tree || $.isEmptyObject(tree)) return ;
	        tree.setting.edit.removeTitle=$i18n("ccay.common.button.delete");
	        tree.selectNode(tree.getNodeByParam("registryItemName",defaultSelect));
    	 });	
	};
	
	/**
	 * 重置元素状态
	 */
	page.resetDom = function(){
		$S("#name").val("");
		$S("#value").val("");
		$S("#desc").val("");
		$S("#valueFromSourceCode").val(""); 
		$S("#valueFromExtInfo").val(""); 
		$S("#name").attr("readOnly",false);
		$S("#value").attr("readOnly",false);
		$S("#savespan").show();
		$S("#valueFromSourceCodeLi").hide();
		$S("#valueFromExtInfoLi").hide();
		$S("#delete").hide();
		$S("#reset").hide();
	}
	
	/**
	 * 单击数据时，form显示数据，数据可修改（更新）
	 */
	page.onTreeClick = function(event, treeId, treeNode) {
		page.resetDom();
		var name = treeNode.registryItemName;
		if(!name){
			return;
		}
		page.loadItem(name);
	};
	
	page.loadItem = function(name){
		Ccay.doGet("services/manage/registry/find",{itemName:name},function(data){
			$S("#name").val(data.name);
			$S("#name").attr("readOnly",true);
			$S("#value").val(data.value);
			$S("#desc").val(data.desc);
			$S("#defineLocation").show();
			if(data.definedInCodeSource){
				$S("#valueFromSourceCodeLi").show(); 
				$S("#valueFromSourceCode").val(data.valueFromSourceCode); 
			}
			if(data.definedInExtStore){
				$S("#valueFromExtInfoLi").show(); 
				$S("#valueFromExtInfo").val(data.valueFromExtInfo); 
			}
			if(!data.overwritable){
				$S("#value").attr("readOnly",true);
				$S("#savespan").hide();
			}
			if(data.definedInCodeSource && data.definedInExtStore){
				$S("#reset").show();
			}
			if(!data.definedInCodeSource && data.definedInExtStore){
				$S("#delete").show();
			}
		});
	}
	/**
	 * 删除
	 */
	page.del = function() {
		var itemName = $S("#name").val().trim();
		Ccay.doDelete("services/manage/registry/delete",
				{itemName : itemName},
				function(data){
					tree.destroy("registryTree");
					page.initRegistryTree();
					page.resetDom();
		});
	}

	/**
	 * 保存编辑信息
	 */
	page.save=function (){
		if(!form.valid())return;
		var params = {
				name : $S("#name").val().trim(),
				value : $S("#value").val().trim(),
				desc : $S("#desc").val().trim()
		}
		Ccay.doPost("services/manage/registry/createOrUpdate",params,function(data){
			tree.destroy("registryTree");
			page.initRegistryTree(params.name);
			page.resetDom();
			page.loadItem(params.name);
		});
	}
	
	/**
	 * 选择节点后在该节点上添加数据  （复位）
	 * 点击的节点为新添加的节点的父节点
	 */
	page.addItem = function() {
			var registryNode=form.jsonData();
			var path = registryNode.parentPath +'.'+ registryNode.name;
			
			if(registryNode.parentPath == "root"){
				path = registryNode.name
			}else{
				path = path.substring(path.indexOf('root.'),path.length);
			}			
	        if(registryNode.propertyId != null){
	        	form.nameChild("parentName").val(registryNode.name);
	    		form.nameChild("parentId").val(registryNode.propertyId);
	    		form.nameChild("parentPath").val(path);
	        }
		    form.nameChild("propertyId").val("");
		    form.nameChild("name").val("");
		    form.nameChild("displayName").val("");	
			form.nameChild("status").attr("checked","checked");			
			form.nameChild("value").val("");
			form.nameChild("listIndex").val("0");
			form.nameChild("description").val("");			
			//name 可写
			form.nameChild("name").attr("readOnly",false);	
			$S("#selectParent").show();
	}

	
	/**
	 *Registry 导出
	 */
	page.exportRegistryByIExcel=function(){
		var parentPath = form.nameChild("parentPath").val();
		if(parentPath == ""){
			parentPath = "root";
		};
		Ccay.doGet("services/ccay/registry/export/single/"+parentPath,'');
	}
	
	page.exit=function(){
		form=null;
	}
	
	/**
	* Registry 导入
	*/
	page.registryMapImport=function(){
		var param={uploadType:"ExcelImport",
					completeCallback: Ccay.Component.Excel.uploadCallback,
					closeDialog:true,
					query:"excelType=ccay.registry&importType=all"};
		Ccay.Component.openUploadDialog("ccay.common.button.import",param);
		
	}
	/**
	* Registry 导入非更新
	*/
	page.registryMapImportWithoutUpdate=function(){
		var param={uploadType:"ExcelImport",
					completeCallback: Ccay.Component.Excel.uploadCallback,
					closeDialog:true,
					query:"excelType=ccay.registry&importType=noUpdate"};
		Ccay.Component.openUploadDialog("ccay.common.button.import",param);
		
	}
	/**
	 * excel导出
	 */
	page.exportRegistry = function(){
		var propertyId = form.nameChild("propertyId").val();
		var jsonData = form.jsonData();
		if(!propertyId || propertyId.length<1){
			jsonData.propertyId = 0;
		}
		Ccay.doGet("services/ccay/registry/exportRegistry",form.jsonData(),function(){
			Ccay.Component.Excel.exportCallback();
		});
	}
});


